package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.table.AbstractTableModel;

public class StatisticTableModel extends AbstractTableModel {
	private static final long serialVersionUID = -5496639638667889566L;
	private ArrayList<Bill> data;
	private ArrayList<ArrayList<String>> subData;
	private ArrayList<String> columnName;

	public StatisticTableModel() {
		this.data = new ArrayList<Bill>();
		this.subData = new ArrayList<>();
		this.columnName = new ArrayList<String>(Arrays.asList("STT",
				"Lo\u1EA1i h\u00F3a \u0111\u01A1n",
				"M\u00E3 h\u00F3a \u0111\u01A1n", "Ng\u00E0y l\u1EADp",
				"\u0110\u1ED1i t\u00E1c", "Nh\u00E2n vi\u00EAn l\u1EADp",
				"T\u1ED5ng gi\u00E1 tr\u1ECB"));
		loadData(null);
	}

	public void loadData(String dk) {
		Connection connect = null;
		try {
			connect = Database.connect();
			data.clear();
			subData.clear();
			String sql = "SELECT bill.*, partner.name, concat(employee.first_name,' ',employee.last_name, ' -ID:', employee.id_empl) as fullname "
					+ "FROM bill INNER JOIN partner ON bill.id_partner = partner.id INNER JOIN employee ON bill.id_employee = employee.id_empl ";
			if (dk != null && !dk.equals(""))
				sql += dk;

			PreparedStatement pre = connect.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();

			while (rs.next()) {
				Bill bill = new Bill();
				bill.setId(rs.getInt("id"));
				bill.setType(rs.getString("type"));
				bill.setDate(rs.getDate("date"));
				bill.setId_partner(rs.getInt("id_partner"));
				bill.setId_detailBill(rs.getInt("id_detailbill"));
				bill.setId_employee(rs.getInt("id_employee"));
				bill.setSumAmount(rs.getInt("sum_amount"));
				bill.setSumMoney(rs.getDouble("sum_money"));
				bill.setSumTax(rs.getDouble("sum_tax"));
				bill.setMoneyPayment(rs.getDouble("money_payment"));
				bill.setPayment(rs.getString("payment"));

				ArrayList<String> tmp = new ArrayList<String>();
				tmp.add(rs.getString("partner.name"));
				tmp.add(rs.getString("fullname"));

				this.subData.add(tmp);
				this.data.add(bill);
			}

			fireTableDataChanged();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public int getRowCount() {
		return this.data.size();
	}

	@Override
	public int getColumnCount() {
		return this.columnName.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.columnName.get(column);
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Bill b = data.get(rowIndex);

		switch (columnIndex) {
		case 0:
			return rowIndex + 1;
		case 1:
			return b.getType();
		case 2:
			return b.getId();
		case 3:
			return b.getDate();
		case 4:
			return subData.get(rowIndex).get(0);
		case 5:
			return subData.get(rowIndex).get(1);
		case 6:
			return b.getMoneyPayment();
		}
		return "";
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case 0:
		case 2:
			return Integer.class;
		case 6:
			return Double.class;
		default:
			return String.class;
		}
	}

}
